package com.cn.newetv.utils;

import java.util.List;

import android.content.Context;

import com.cn.newetv.po.FavDataBaseDO;
import com.cn.newetv.po.FavFilmDataBaseDO;
import com.cn.newetv.po.VFilmDO;

public class SystemCommon {
	private static SystemCommon self = null;
	private SysCfgDao sysCfgDao = null;
	private FavListDao favListDao = null;
	private FavFilmDao favFilmDao = null;
	private SystemCommon() {
		
	}
	private SystemCommon(Context context) {
		try {
			sysCfgDao = new SysCfgDao();
			sysCfgDao.openDB(context);
			favListDao = new FavListDao();
			favListDao.openDB(context);
			favFilmDao = new FavFilmDao();
			favFilmDao.openDB(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static SystemCommon getInstance(Context context) {
		if(self == null)
			self = new SystemCommon(context);
		return self;
	}
	public void setVersionData() {
        try {
			String res = sysCfgDao.getByKey("version");
			String ver = ETVConstant.APP_VERSION;
			if(res == null)
				sysCfgDao.add("version", ver);
			else
				sysCfgDao.modify("version", ver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean validActived() {
		try {
			String res = sysCfgDao.getByKey("isactive");
			if("0".equals(res))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean validIsFirstPwd() {
		try {
			String res = sysCfgDao.getByKey("isfirstpwd");
			if("1".equals(res))
				return false;
			else
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
	
	public boolean validIsSavePwd() {
		try {
			String res = sysCfgDao.getByKey("issavepwd");
			if("1".equals(res))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public void saveFirstPwd() {
		try {
			String res = sysCfgDao.getByKey("isfirstpwd");
			if(res == null)
				sysCfgDao.add("isfirstpwd", "1");
			else
				sysCfgDao.modify("isfirstpwd", "1");
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void saveIsSavePwd(boolean issave) {
		try {
			String res = sysCfgDao.getByKey("issavepwd");
			if(res == null)
				sysCfgDao.add("issavepwd", issave?"1":"0");
			else
				sysCfgDao.modify("issavepwd", issave?"1":"0");
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void saveParentPwd(String parentpwd) {
		try {
			String res = sysCfgDao.getByKey("parentpwd");
			if(res == null)
				sysCfgDao.add("parentpwd", parentpwd);
			else
				sysCfgDao.modify("parentpwd", parentpwd);
				
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public String getParentPwd() {
		return sysCfgDao.getByKey("parentpwd");
	}
	
	public List<FavDataBaseDO> getFavList(Integer mode) {
		return favListDao.findAll(mode);
	}
	
	public List<FavDataBaseDO> getFavListByFilmId(String filmId,Integer mode) {
		return favListDao.getByFilmId(filmId, mode);
	}
	
	public void addBookMark(FavDataBaseDO favDataBaseDO, String filmId, Integer mode) {
		FavDataBaseDO res = favListDao.getByName(favDataBaseDO.getVideoname(), mode);
		if(res == null)
			favListDao.insert(favDataBaseDO, filmId, mode);
	}
	
	public void deleteBookMark(Integer id) {
		favListDao.deleteById(id);
	}
	
	public void deleteBookMark(String name, Integer mode) {
		favListDao.deleteByName(name,mode);
	}
	
	public List<VFilmDO> getFavFilmPage(int pagesize, int page, Integer mode) {
		return favFilmDao.findPage(pagesize, page-1, mode);
	}
	
	public List<VFilmDO> getFavFilms(Integer mode) {
		return favFilmDao.findAll(mode);
	}
	
	public void addFavFilm(FavFilmDataBaseDO favFilmDataBaseDO, Integer mode) {
		FavFilmDataBaseDO res =favFilmDao.getById(favFilmDataBaseDO.getId(), mode);
		if(res == null)
			favFilmDao.insert(favFilmDataBaseDO, mode);
	}
	
	public void deleteFavFilm(String filmId, Integer mode) {
		List<FavDataBaseDO> res = favListDao.getByFilmId(filmId, mode);
		if(res == null || res.size() == 0)
			favFilmDao.deleteById(filmId, mode);
	}
	
	public void deleteBookMarkFavFilm(String filmId, Integer mode) {
		favListDao.deleteByFilmId(filmId, mode);
		favFilmDao.deleteById(filmId, mode);
	}
}
